package com.example.cashier

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.cashier.adapter.CashierAdapter
import com.example.cashier.databinding.ActivityCashierBinding
import com.example.cashier.entity.CashierEntityItem
import com.example.cashier.vm.CashierViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/cashier/CashierActivity")
class CashierActivity : BaseMvvmActivity<ActivityCashierBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_cashier
    var cashierAdapter=CashierAdapter()
    var paylist= listOf<CashierEntityItem>()
    val cashierViewModel:CashierViewModel by viewModels()
    var month= SimpleDateFormat("MM").format(System.currentTimeMillis())
    var year=SimpleDateFormat("yyyy").format(System.currentTimeMillis())
    var type=""
    override fun initView() {
        super.initView()
        //返回
        mDataBinding.icBack.setOnClickListener {
            finish()
        }
        mDataBinding.rvPaylist.adapter=cashierAdapter
        mDataBinding.rvPaylist.layoutManager= LinearLayoutManager(this)
        mDataBinding.rgDate.setOnCheckedChangeListener { group, checkedId ->
            cashierAdapter.data.clear()
            when(checkedId){
                R.id.rb1->{
                    cashierAdapter.addData(paylist.filter { it.paymentTime.split("-")[1]==month})
                }
                R.id.rb2->{
                    cashierAdapter.addData(paylist.filter { it.paymentTime.split("-")[0]==year})
                }
            }
            cashierAdapter.notifyDataSetChanged()
        }
        var arr= mutableListOf<String>("全部", "物业费", "停车费","住宿费","水电费")
        var adpter=ArrayAdapter(this,android.R.layout.simple_spinner_item,arr)
        adpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.paylistProject.adapter=adpter
        mDataBinding.paylistProject.onItemSelectedListener= object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long,
            ) {
                cashierAdapter.data.clear()
                type=arr[position]
                if (position==0){
                    cashierViewModel.getCashier(4)
                }else{
                    cashierAdapter.addData(paylist.sortedByDescending { it.paymentTime }.filter { it.chargeType==type})
                }
                cashierAdapter.notifyDataSetChanged()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
        }
        cashierViewModel.mSuccessLiveData.observe(this){
            cashierAdapter.data.clear()
            paylist=it.data
            cashierAdapter.addData(paylist.sortedByDescending { it.paymentTime }.filter { it.paymentTime.split("-")[1]==month})
            cashierAdapter.notifyDataSetChanged()
        }
        cashierAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build( "/cashier/CashierXaingActivity")
                .withSerializable("cashier",cashierAdapter.data[position])
                .navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        cashierViewModel.getCashier(4)
    }
}
